const app = getApp()
Page({
  data: {
    solitaire: [],
    imgbgsrc:'',
    loading: false,
    userInfo: app.globalData.userInfo
  },
  onLoad: function (options) {
    const that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(data)
      if (data.data.sku.images){
        var imgstr = JSON.parse(data.data.sku.images)
        console.log(imgstr)
      }
      that.setData({
        solitaire: data.data,
        itemPic: imgstr
      })
      that.getpic()
    })
    
  },
  onShareAppMessage: function () {
    console.log('/pages/shareSolitaireD/shareSolitaireD?solitaireId=' + this.data.solitaire.solitaireId + '&userId=' + app.globalData.userId)
    // console.log(this.data.solitaire.sku.image)
    return {
      title: app.globalData.userInfo.nickname + '邀请你参加接龙',
      path: '/pages/shareSolitaireD/shareSolitaireD?solitaireId=' + this.data.solitaire.solitaireId + '&userId=' + app.globalData.userId,
      imageUrl: this.data.shareimg1
    }
  },
  getpic(){
    wx.showLoading({
      title: '接龙生成中...',
    })
    const that = this
    var imgbgsrc = this.data.solitaire.sku.image
    wx.getImageInfo({
      src: imgbgsrc,
      success: function (res) {
        that.setData({
          imgbgsrc: res.path
        })
        console.log(that.data.imgbgsrc)
      }
    })
    let promise = new Promise((resolve, reject) => {
      let timer = setInterval(() => {
        // console.log(app.globalData.sessionId,app.globalData.userId)
        if (that.data.imgbgsrc !== '') {
          clearInterval(timer);
          resolve(that.data);
          that.getSharePic()
        } else {

        }
      }, 250);
    });
  },
  getSharePic(){
    const that = this
    var str1 = this.data.solitaire.title
    var str2 = '我要接龙'
    const sharepic = wx.createCanvasContext('myCanvas')
    console.log(that.data.imgbgsrc)
    sharepic.drawImage(that.data.imgbgsrc, 0, -40, 500, 500)
    sharepic.setFillStyle('#fff')
    sharepic.fillRect(0, 0, 500, 40)
    sharepic.setFillStyle('#fff')
    sharepic.fillRect(0, 320, 500, 80)
    sharepic.setTextAlign('left')
    sharepic.setFontSize(20)
    sharepic.setFillStyle('#333')
    sharepic.fillText(str1, 20, 25)
    sharepic.arc(50, 360, 30, 0, Math.PI * 2, false);
    sharepic.setFillStyle('#f95050')
    sharepic.fill()
    sharepic.arc(450, 360, 30, 0, Math.PI * 2, false);
    sharepic.setFillStyle('#f95050')
    sharepic.fill()
    sharepic.setFillStyle('#f95050')
    sharepic.fillRect(50, 330, 400, 60)
    sharepic.setFontSize(26)
    sharepic.setTextAlign('center')
    sharepic.setFillStyle('#fff')
    sharepic.fillText(str2, 250, 370)
    sharepic.draw(false, function (e) {
      console.log(e)
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 500,
        height: 400,
        destWidth: 500,
        destHeight: 400,
        canvasId: 'myCanvas',
        success: function (res) {
          console.log(res.tempFilePath)
          that.setData({
            shareimg1: res.tempFilePath,
            imgShow: true,
            loading: true,
          });
          wx.hideLoading()
        },
        fail(res) {
          console.log(res)
        }
      })
    })
  }
})